package cn.ethan.demo.design.factory;

import cn.ethan.demo.design.util.ClassLoaderUtils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author 张俊辉
 */

public class JDKInvocationHandler implements InvocationHandler {

    private CacheAdapter cacheAdapter;

    public JDKInvocationHandler(CacheAdapter cacheAdapter) {
        this.cacheAdapter = cacheAdapter;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("JDKInvocationHandler invoke method: " + method.getName());

        Method method1 = CacheAdapter.class.getMethod(method.getName(), ClassLoaderUtils.getClazzByArgs(args));
        System.out.println("JDKInvocationHandler invoke method1: " + method1.getName());

        Object invoke1 = method1.invoke(cacheAdapter, args);

//        Object invoke = CacheAdapter.class
//                .getMethod(method.getName(), ClassLoaderUtils.getClazzByArgs(args))
//                .invoke(cacheAdapter, args);
        System.out.println("JDKInvocationHandler invoke: " + invoke1);
        return invoke1;
    }
}
